﻿using Microsoft.Practices.Prism.Mvvm;
using Microsoft.Practices.Prism.Regions;
using System;

namespace Songhay.Mvvm.Extensions
{
    public static class BindableBaseExtensions
    {
        /// <summary>
        /// Gets the identifier parameter.
        /// </summary>
        /// <param name="viewModel">The view model.</param>
        /// <param name="navigationContext">The navigation context.</param>
        public static int GetIdParameter(this BindableBase viewModel, NavigationContext navigationContext)
        {
            var id = Convert.ToInt32(navigationContext.Parameters["id"]);
            return id;
        }

        /// <summary>
        /// Determines whether the specified View Model is the Prism Navigation target.
        /// </summary>
        /// <param name="viewModel">The view model.</param>
        /// <param name="navigationContext">The navigation context.</param>
        public static bool IsINavigationAwareNavigationTarget(this BindableBase viewModel, NavigationContext navigationContext)
        {
            if (viewModel == null) return false;
            if (navigationContext == null) return false;

            var key = viewModel.GetType().Name.Replace("Model", string.Empty);
            return navigationContext.Uri.OriginalString.Contains(key);
        }
    }
}
